package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;

import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealPageVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分页查询
     * */
    @Override
    public PageResult pageAll(SetmealPageQueryDTO setmealPageQueryDTO){
        //根据条件查询所有数据
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        if (setmealPageQueryDTO.getCategoryId() != null){
             setmeal = Setmeal.builder()
                    .categoryId(Long.valueOf(setmealPageQueryDTO.getCategoryId()))
                    .name(setmealPageQueryDTO.getName())
                    .status(setmealPageQueryDTO.getStatus()).build();
        }
        BeanUtils.copyProperties(setmealPageQueryDTO,setmeal);
        List<SetmealPageVO> setmealPageVOList = setmealMapper.pageList(setmeal);
        for (SetmealPageVO setmealPageVO : setmealPageVOList) {
            String name = categoryMapper.selectName(setmealPageVO.getCategoryId());
            setmealPageVO.setCategoryName(name);
        }
        PageInfo<SetmealPageVO> setmealVOPageInfo = new PageInfo<>(setmealPageVOList);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmealVOPageInfo.getTotal());
        pageResult.setRecords(setmealPageVOList);
        return pageResult;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * */
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.add(setmealDish);
        }
    }

    /**
     * 批量删除套餐
     * */
    @Override
    public void deleteById(String ids) {
        String[] strId = ids.split(",");
        Long id;
        for (String s : strId) {
            id = Long.valueOf(s);
            setmealMapper.deleteById(id);
        }
    }

    /**
     * 根据id查询套餐
     * */
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }
    /**
     * 套餐起售、停售
     * */
    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.updateStatus(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 修改套餐
     * */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
           setmealDishMapper.add(setmealDish);
        }
    }
}
